package shujia.day08;

/*
    字符串反转
    举例：键盘录入”abc”		输出结果：”cba”

    方式1：将字符串转字符数组，数组逆序，再转成字符串
    方式2：将字符串转字符数组，倒着遍历，拼接成一个字符串
    方式3：先将字符串转StringBuffer,调用StringBuffer中的反转方法，再转成字符串
 */
public class StringTest4 {
    public static void main(String[] args) {
        String s1 = "我爱莫提冯";
//        System.out.println(fun1(s1));

//        System.out.println(fun2(s1));
        System.out.println(fun3(s1));

    }

    public static String fun3(String s){
        // String -> StringBuffer
        StringBuffer sb = new StringBuffer(s);
        sb.reverse();
        // StringBuffer -> String
        return sb.toString();
    }

    public static String fun2(String s) {
        char[] chars = s.toCharArray();
        String res = "";
        for (int i = chars.length - 1; i >= 0; i--) {
            res+=chars[i];
        }

        return res;
    }

    public static String fun1(String s) {
        char[] chars = s.toCharArray();
        nuXu(chars);
        return new String(chars);
    }

    public static void nuXu(char[] chars) {
        for (int front = 0, end = chars.length - 1; front < end; front++, end--) {
            char tmp = chars[front];
            chars[front] = chars[end];
            chars[end] = tmp;
        }
    }
}
